package com.spring.lesson001.demo6;

/**
 * @author vhans
 */
public class DiAutowireByConstructor {
    public static class BaseService {
        private String desc;

        public String getDesc() {
            return this.desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        @Override
        public String toString() {
            return "BaseService{" +
                    "desc='" + desc + '\'' +
                    '}';
        }
    }

    public static class Service1 extends BaseService {
    }

    public static class Service2 extends BaseService {
    }

    private Service1 service1;
    private Service2 service2;

    // 构造函数匹配采用贪婪匹配，多个构造函数按参数数逆序排序，spring容器根据配置找到一个合适的构造函数
    public DiAutowireByConstructor() {}

    public DiAutowireByConstructor(Service1 service1) {
        System.out.println("DiAutowireByConstructor(Service1 service1)");
        this.service1 = service1;
    }

    public DiAutowireByConstructor(Service2 service2) {
        System.out.println("DiAutowireByConstructor(Service2 service2)");
        this.service2 = service2;
    }

    public DiAutowireByConstructor(Service1 service1, Service2 service2) {
        System.out.println("DiAutowireByConstructor(Service1 service1, Service2 service2)");
        this.service1 = service1;
        this.service2 = service2;
    }

    @Override
    public String toString() {
        return "DiAutowireByConstructor{" +
                "service1=" + service1 +
                ", service2=" + service2 +
                '}';
    }
}
